package com.billionsfinance.hessian.core;

import com.billionsfinance.hessian.model.ClientInfo;
import com.billionsfinance.hessian.model.ServerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Hessian接口缓存管理
 *
 * @FileName: HessianCacheManager.java
 * @author:WeiHui.Zhang
 * @Date: 2016年6月8日 上午11:13:33
 * @Version:V1.00
 */
public class HessianCacheManager {

	private static final Logger LOGGER = LoggerFactory.getLogger(HessianCacheManager.class);

	private static Map<String, ServerInfo> SERVER_INFO_MAP = new ConcurrentHashMap<>();
	private static Map<String, ClientInfo> Client_INFO_MAP = new ConcurrentHashMap<>();

	private static Map<String, Class<?>> HESSIAN_SERVER_INTERFACE_MAP = new ConcurrentHashMap<>();
	private static Map<String, Class<?>> HESSIAN_SERVER_CLASS_MAP = new ConcurrentHashMap<>();

	private static Map<String, Class<?>> HESSIAN_CLIENT_INTERFACE_MAP = new ConcurrentHashMap<>();

	/**
	 * 缓存服务端信息
	 *
	 * @param accessPath 接口名
	 * @param serverInfo 服务端信息
	 */
	public static void setServerInfo(String accessPath, ServerInfo serverInfo) {
		SERVER_INFO_MAP.put(accessPath, serverInfo);
		try {
			HESSIAN_SERVER_INTERFACE_MAP.put(serverInfo.getInterfaceName(), Class.forName(serverInfo.getInterfaceName()));
			HESSIAN_SERVER_CLASS_MAP.put(serverInfo.getServiceClassName(), Class.forName(serverInfo.getServiceClassName()));
		} catch (ClassNotFoundException e) {
			LOGGER.error("缓存Hessian服务端配置出错.", e);
		}
	}

	/**
	 * 缓存客户端信息
	 *
	 * @param clientInfo 客户端信息
	 */
	public static void setClientInfo(ClientInfo clientInfo) {
		Client_INFO_MAP.put(clientInfo.getInterfaceName(), clientInfo);
		try {
			HESSIAN_CLIENT_INTERFACE_MAP.put(clientInfo.getInterfaceName(), Class.forName(clientInfo.getInterfaceName()));
		} catch (ClassNotFoundException e) {
			LOGGER.error("缓存Hessian客户端配置出错.", e);
			throw new RuntimeException("缓存Hessian客户端配置出错.", e);
		}
	}

	public static ServerInfo getServerInfo(String accessPath) {
		return SERVER_INFO_MAP.get(accessPath);
	}

	public static ClientInfo getClientInfo(String interfaceName) {
		return Client_INFO_MAP.get(interfaceName);
	}

	public static Class<?> getHessianClientInterfaceClass(String interfaceName) {
		return HESSIAN_CLIENT_INTERFACE_MAP.get(interfaceName);
	}

	public static Class<?> getHessianServerInterfaceClass(String interfaceName) {
		return HESSIAN_SERVER_INTERFACE_MAP.get(interfaceName);
	}

	public static Class<?> getHessianServerClass(String className) {
		return HESSIAN_SERVER_CLASS_MAP.get(className);
	}

}